home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / System / ScreenDaemon 1.2 / ScreenDaemon appe / ScreenDaemon.c < prev    next >
C/C++ Source or Header  |  1996-05-07  |  11KB  |  381 lines

  1. /*********************************************************************
  2.  * ScreenDaemon
  3.  * Copyright ©1994-1995 by Mason L. Bliss
  4.  * All Rights Reserved
  5.  *
  6.  * A simple gamma-table-fading screen saver.
  7.  *
  8.  * Version 1.1 2/19/1995 by Mason L. Bliss
  9.  *
  10.  *********************************************************************/
  11.  
  12. #include <AppleEvents.h>
  13. #include <GestaltEqu.h>
  14. #include <LowMem.h>
  15. #include <Palettes.h>
  16. #include "SD.h"
  17. #include "ScreenDaemon.h"
  18. #include "gamma.h"
  19.  
  20. #define kDelayTicks    45        /* = three-quarters of a second */
  21. #define kFadeRate    5        /* must divide evenly into 100 */
  22.  
  23. Boolean             gRunning = true,
  24.                     gBadStart = false;
  25. long                gLastNap;
  26. AEEventHandlerUPP    DoAEQuitAppUPP,
  27.                     DoAEOpenDocUPP,
  28.                     DoAEPrintDocUPP,
  29.                     DoAEOpenAppUPP;
  30. PatchGlobalsPtr        pgPtr = NULL;
  31.  
  32. /*********************************************************************
  33.  * main:
  34.  *
  35.  * Set up our daemon and then drop into the main event loop.
  36.  *
  37.  *********************************************************************/
  38. void main(void)
  39. {
  40.     RgnHandle    cursorRgn = NULL;
  41.     EventRecord    theEvent;
  42.     
  43.     /* Initialize appropriate managers and install AppleEvent handlers and stuff */
  44.     SetUp();
  45.  
  46.     /* Main event loop */
  47.     while (gRunning) {
  48.         WaitNextEvent(everyEvent, &theEvent, 30, cursorRgn);    /* Get an event... */
  49.         HandleEvent(&theEvent);                                    /* ...and process it. */
  50.     }
  51.  
  52.     /* Remove our AppleEvent handlers and the gamma tools and such */
  53.     CleanUp();
  54. }
  55.  
  56.  
  57.  
  58. /*********************************************************************
  59.  * SetUp:
  60.  *
  61.  * Set up the appropriate managers and stuff.
  62.  *
  63.  *********************************************************************/
  64. void SetUp(void)
  65. {
  66.     long        result;
  67.     GDHandle    theDevice;
  68.     Rect        screenRect;
  69.  
  70. #if GENERATINGPOWERPC
  71.     ReleaseResource(GetResource('CODE',1));
  72. #endif
  73.  
  74.     MaxApplZone();                    /* Take over the whole partition */
  75.  
  76.     InitGraf(&qd.thePort);
  77.  
  78.     DoAEQuitAppUPP = NewAEEventHandlerProc(DoAEQuitApp);
  79.     DoAEOpenDocUPP = NewAEEventHandlerProc(DoAEOpenDoc);
  80.     DoAEPrintDocUPP = NewAEEventHandlerProc(DoAEPrintDoc);
  81.     DoAEOpenAppUPP = NewAEEventHandlerProc(DoAEOpenApp);
  82.  
  83.     if (AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, DoAEOpenAppUPP, 0, false) != noErr ||
  84.         AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, DoAEQuitAppUPP, 0, false) != noErr ||
  85.         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, DoAEOpenDocUPP, 0, false) != noErr ||
  86.         AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, DoAEPrintDocUPP, 0, false) != noErr) {
  87.         SysBeep(5);
  88.         gRunning = false;
  89.         gBadStart = true;
  90.     }
  91.  
  92.     /* Try to get the address of our patch globals */
  93.     if (Gestalt('sDmn', &result) == noErr) {
  94.         pgPtr = (PatchGlobalsPtr) result;
  95.         SetupGammaTools();
  96.     } else {
  97.         gRunning = false;
  98.     }
  99.  
  100.     /* Get our screen coordinates */
  101.     theDevice = LMGetMainDevice();
  102.     screenRect = (*(*theDevice)->gdPMap)->bounds;
  103.     
  104. //    BlockMoveData((Ptr) &((**((PixMapHandle) (**theDevice).gdPMap)).bounds),
  105. //            &screenRect, sizeof(Rect));
  106.     
  107.     pgPtr->pgCorners[0].left = screenRect.left - 1;        // top left
  108.     pgPtr->pgCorners[0].right = screenRect.left + 8;
  109.     pgPtr->pgCorners[0].top = screenRect.top - 1;
  110.     pgPtr->pgCorners[0].bottom = screenRect.top + 8;
  111.  
  112.     pgPtr->pgCorners[1].left = screenRect.right - 8;    // top right
  113.     pgPtr->pgCorners[1].right = screenRect.right + 1;
  114.     pgPtr->pgCorners[1].top = screenRect.top - 1;
  115.     pgPtr->pgCorners[1].bottom = screenRect.top + 8;
  116.  
  117.     pgPtr->pgCorners[2].left = screenRect.right - 8;    // bottom right
  118.     pgPtr->pgCorners[2].right = screenRect.right + 1;
  119.     pgPtr->pgCorners[2].top = screenRect.bottom - 8;
  120.     pgPtr->pgCorners[2].bottom = screenRect.bottom + 1;
  121.  
  122.     pgPtr->pgCorners[3].left = screenRect.left - 1;        // bottom left
  123.     pgPtr->pgCorners[3].right = screenRect.left + 8;
  124.     pgPtr->pgCorners[3].top = screenRect.bottom - 8;
  125.     pgPtr->pgCorners[3].bottom = screenRect.bottom + 1;
  126. }
  127.  
  128.  
  129.  
  130.         
  131. /*********************************************************************
  132.  * HandleEvent:
  133.  *
  134.  * Take care of the event that's been passed to us.
  135.  *
  136.  *********************************************************************/
  137. void HandleEvent(EventRecord *theEvent)
  138. {
  139.     /* What did we get? */
  140.     switch (theEvent->what) {
  141.     
  142.         /* Take care of any incoming AppleEvents. */
  143.         case kHighLevelEvent:
  144.             AEProcessAppleEvent(theEvent);
  145.             break;
  146.         
  147.         /* We do all our work during our idle time. */
  148.         case nullEvent:
  149.             DoMaintenance();
  150.             break;
  151.         
  152.         default:
  153.             break;
  154.     }
  155. }
  156.  
  157.  
  158.  
  159. /*********************************************************************
  160.  * DoMaintenance:
  161.  *
  162.  * Do our periodic maintenance.
  163.  *
  164.  *********************************************************************/
  165. void DoMaintenance(void)
  166. {
  167.     Point    mousePt;
  168.  
  169.     /* Check to see if the mouse has moved */
  170.     GetMouse(&mousePt);
  171.     LocalToGlobal(&mousePt);
  172.     if (!EqualPt(mousePt, pgPtr->pgLastMouse)) {    /* id est, mouse moved */
  173.         pgPtr->pgLastMouse = mousePt;                /* Update last position */
  174.         pgPtr->pgLastAction = LMGetTicks();                /* Record the time */
  175.         pgPtr->pgInSleepRect = false;                /* We're not narcoleptic */
  176.         if (pgPtr->pgSaverOn)
  177.             pgPtr->pgMustWake = true;                /* Wake up if we need to */
  178.     }
  179.     
  180.     if (!pgPtr->pgSaverOn) {    /* wide-awake maintenance */
  181.     
  182.         /* Does the nice user want us to fall asleep now? */
  183.         if (!pgPtr->pgInSleepRect &&
  184.                     PtInRect(mousePt, &pgPtr->pgCorners[pgPtr->pgSleepRect])) {
  185.             pgPtr->pgInSleepRect = true;
  186.             
  187.             /* Make it look like we haven't moved for a while. */
  188.             pgPtr->pgLastAction -= ((pgPtr->pgIdleTicks) - kDelayTicks);
  189.             
  190.         } else {
  191.         
  192.             /* If we're in the wake rect, we never want to sleep */
  193.             if (PtInRect(mousePt, &pgPtr->pgCorners[pgPtr->pgWakeRect]))
  194.                 pgPtr->pgLastAction = LMGetTicks();
  195.         }
  196.         
  197.         /* Has enough idle time passed for us to fall asleep naturally? */
  198.         if ((LMGetTicks() - pgPtr->pgLastAction) > pgPtr->pgIdleTicks)
  199.             pgPtr->pgMustSleep = true;
  200.             
  201.         /* Do we need to fall asleep? */
  202.         if (pgPtr->pgMustSleep && pgPtr->pgMustSave)
  203.             FallAsleep();
  204.  
  205.     } else {    /* We're asleep - do sleepy-time maintenance. */
  206.     
  207.         if (pgPtr->pgMustWake)
  208.             WakeUp();
  209.         else if (LMGetTicks() - gLastNap > 3600) {    /* once per minute */
  210.             gLastNap = LMGetTicks();
  211.             DoGammaFade(0);        /* black out the screen again, "just in case" */
  212.         }
  213.     }
  214. }
  215.  
  216.  
  217.  
  218. /*********************************************************************
  219.  * CleanUp:
  220.  *
  221.  * Remove our Apple Event Handlers and stuff.
  222.  *
  223.  *********************************************************************/
  224. void CleanUp(void)
  225. {
  226.     /* Unload the gamma tools, if we've already loaded them. */
  227.     if (pgPtr != NULL)
  228.         DisposeGammaTools();
  229.  
  230.     /* Remove the core AppleEvent handlers */
  231.     if ((AEEventHandlerUPP) DoAEOpenApp)
  232.         AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
  233.                     (AEEventHandlerUPP) DoAEOpenApp, false);
  234.                     
  235.     if ((AEEventHandlerUPP) DoAEQuitApp)
  236.         AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
  237.                     (AEEventHandlerUPP) DoAEQuitApp, false);
  238.  
  239.     if ((AEEventHandlerUPP) DoAEOpenDoc)
  240.         AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
  241.                     (AEEventHandlerUPP) DoAEOpenDoc, false);
  242.  
  243.     if ((AEEventHandlerUPP) DoAEPrintDoc)
  244.         AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
  245.                     (AEEventHandlerUPP) DoAEPrintDoc, false);
  246.  
  247.     /* Dispose of our Universal Procedure Pointers */
  248.     if (DoAEQuitAppUPP)
  249.         DisposeRoutineDescriptor(DoAEQuitAppUPP);
  250.     if (DoAEOpenDocUPP)
  251.         DisposeRoutineDescriptor(DoAEOpenDocUPP);
  252.     if (DoAEPrintDocUPP)
  253.         DisposeRoutineDescriptor(DoAEPrintDocUPP);
  254. }
  255.  
  256.  
  257.  
  258. /*********************************************************************
  259.  * FallAsleep:
  260.  *
  261.  * Fade the screen.
  262.  *
  263.  *********************************************************************/
  264. void FallAsleep(void)
  265. {
  266.     long        level = 100;
  267.  
  268.     /* Set our flags appropriately */
  269.     gLastNap = LMGetTicks();                /* Set "now" as the last time we've faded. */
  270.     pgPtr->pgSaverOn = true;        /* We're now asleep. */
  271.     pgPtr->pgMustSleep = false;        /* We don't have to fall asleep again. */
  272.     pgPtr->pgInSleepRect = false;    /* The mouse doesn't care if it's in the sleep */
  273.                                     /* corner any more. */
  274.  
  275.     /* Dim the screen */
  276.     while ((level -= kFadeRate) >= 0)
  277.         DoGammaFade(level);
  278. }
  279.  
  280.  
  281.  
  282. /*********************************************************************
  283.  * WakeUp:
  284.  *
  285.  * Fade the screen.
  286.  *
  287.  *********************************************************************/
  288. void WakeUp(void)
  289. {
  290.     long            level = 0;
  291.     OSErr            theError;
  292.     GDHandle        theDevice;
  293.     short            theDepth, theFlags;
  294.     PixMapHandle    thePixMap;
  295.  
  296.     /* Unfade */
  297.     while ((level += kFadeRate) <= 100)
  298.         DoGammaFade(level);
  299.  
  300.     /* Reset some flags. */
  301.     pgPtr->pgMustWake = false;
  302.     pgPtr->pgSaverOn = false;
  303.     pgPtr->pgLastAction = LMGetTicks() + 60;    /* can't sleep until a second from now */
  304.  
  305.     /* Force all monitors to redraw, if required */
  306.     if (pgPtr->pgForceUpdates) {
  307.         /* Force all monitors to redraw */
  308.         theDevice = GetMainDevice();
  309.         do {
  310.             thePixMap = (*theDevice)->gdPMap;
  311.             theDepth = (*thePixMap)->pixelSize;
  312.             theFlags = (*theDevice)->gdFlags;
  313.             theError = SetDepth(theDevice, theDepth, 1, theFlags);
  314.         } while ((theDevice = (GDHandle) (*theDevice)->gdNextGD) != NULL);
  315.  
  316.         DrawMenuBar();
  317.     }
  318. }
  319.  
  320.  
  321.  
  322. /*********************************************************************
  323.  * DoAEOpenApp:
  324.  *
  325.  * Handle "Open Application" AppleEvents.
  326.  *
  327.  *********************************************************************/
  328. pascal OSErr DoAEOpenApp(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent, long refCon)
  329. {
  330. #pragma unused (theAppleEvent,replyAppleEvent,refCon)
  331.     return noErr;
  332. }
  333.  
  334.  
  335.  
  336. /*********************************************************************
  337.  * DoAEQuitApp:
  338.  *
  339.  * Handle "Quit" AppleEvents.
  340.  *
  341.  *********************************************************************/
  342. pascal OSErr DoAEQuitApp(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent, long refCon)
  343. {
  344. #pragma unused (theAppleEvent,replyAppleEvent,refCon)
  345.     gRunning = false;
  346.  
  347.     if (pgPtr->pgSaverOn == true)
  348.         WakeUp();
  349.     
  350.     return noErr;
  351. }
  352.  
  353.  
  354.  
  355. /*********************************************************************
  356.  * DoAEOpenDoc:
  357.  *
  358.  * Handle "Open Document" AppleEvents.
  359.  *
  360.  *********************************************************************/
  361. pascal OSErr DoAEOpenDoc(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent,
  362.                                                                     long refCon)
  363. {
  364. #pragma unused (theAppleEvent,replyAppleEvent,refCon)
  365.     return errAEEventNotHandled;
  366. }
  367.  
  368.  
  369.  
  370. /*********************************************************************
  371.  * DoAEPrintDoc:
  372.  *
  373.  * Handle "Print Document" AppleEvents.
  374.  *
  375.  *********************************************************************/
  376. pascal OSErr DoAEPrintDoc(const AppleEvent *theAppleEvent, AppleEvent *replyAppleEvent,
  377.                                                                     long refCon)
  378. {
  379. #pragma unused (theAppleEvent,replyAppleEvent,refCon)
  380.     return errAEEventNotHandled;
  381. }